package cao.oschina.ui;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;

import cao.oschina.R;
import cao.oschina.activity.MainActivity;
import cao.oschina.function.FunctionFragment;
import cao.oschina.function.found.FoundFragment;
import cao.oschina.function.me.MeFragment;
import cao.oschina.function.multiple.MultipleFragment;
import cao.oschina.function.tweet.TweetFragment;
import cao.oschina.widget.TabItemView;
import cao.oschina.widget.TabView;

/**
 * 主页
 * <p/>
 * Created by Haidy on 15/3/2.
 */
public class Main extends MainActivity {

    private FunctionFragment[] functionFragments = new FunctionFragment[4];
    private int showIndex = 0;

    private FragmentManager fragmentManager;
    private FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    /**
     * 初始化
     */
    private void init() {
        initView();
        initData();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        setSideBarView(R.layout.sidebar_view);
        setBody(R.layout.main_view);
//        flipper = (ViewFlipper) getBody().findViewById(R.id.main_flipper);
        TabView tabView = (TabView) getBody().findViewById(R.id.tab_view);
        //第一个Tab
        tabView.setOnCheckedChangedListener(new TabView.OnCheckedChangedListener() {
            @Override
            public void onChanged(TabItemView tabItemView) {
                fragmentManager = getFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.hide(functionFragments[showIndex]);
                int index = tabItemView.getId() - R.id.mul_tab;
                if (functionFragments[index] == null) {
                    if (index == 1) {
                        functionFragments[index] = new TweetFragment(Main.this);
                        fragmentTransaction.add(R.id.id_content, functionFragments[index]);
                    } else if (index == 2) {
                        functionFragments[index] = new FoundFragment(Main.this);
                        fragmentTransaction.add(R.id.id_content, functionFragments[index]);
                    } else if (index == 3) {
                        functionFragments[index] = new MeFragment(Main.this);
                        fragmentTransaction.add(R.id.id_content, functionFragments[index]);
                    }
                }
                fragmentTransaction.show(functionFragments[index]);
                fragmentTransaction.commit();
                showIndex = index;
            }

            @Override
            public void onOtherClicked(View view) {
                System.out.println("菜单");
            }
        });
    }


    /**
     * 初始化数据
     */
    private void initData() {
        functionFragments[0] = new MultipleFragment(this);
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.id_content, functionFragments[0]);
        fragmentTransaction.commit();
    }
}
